package com.liyueheng.service.impl;

import com.liyueheng.mapper.UserMapper;
import com.liyueheng.pojo.User;
import com.liyueheng.service.UserService;
import com.liyueheng.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    // 这里可以注入 UserMapper 或者其他依赖
    @Autowired
    private UserMapper userMapper; // 假设 UserMapper 是一个数据访问层接口


    @Override
    public User findByUserName(String username) {
        // 实现查询用户的逻辑
        User u =  userMapper.findByUsername(username);

        return u; // 返回查询结果
    }

    @Override
    public void register(String username, String password) {
        // 防御性编程，参数校验
        if(username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()){
            throw new IllegalArgumentException("用户名和密码不能为空");
        }
        // 对密码进行加密处理
        String md5String = Md5Util.getMD5String(password);
        // 添加
        userMapper.add(username, md5String);
    }
}
